package{
    /**
     *@author:Childhood
     *@website:flashas3.diandian.com
     */
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    public class CoreDemo extends Sprite{
        private var _pressed:Boolean = false;
        private var oldX:Number;
        private var oldY:Number;
        private static const LINE_COLOR:uint = 0xff0000;
        private static const CANVAS_WIDTH:uint = 760;
        private static const CANVAS_HEIGHT:uint = 480;
        //功能
        private static const FREE_HAND:uint = 1;
        private static const LINE:uint = 2;
        private var lineColor:uint;
        private var statusId:uint;

        private var canvas:Sprite;
        private var shapeArr:Array;
        private var redoList:Array;
        public function CoreDemo():void{
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            addEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
        }

        private function onAddedToStage(event:Event):void{
            removeEventListener(Event.ADDED_TO_STAGE,onAddedToStage);
            init();
        }

        private function init():void{
            statusId = FREE_HAND;
            shapeArr = new Array();
            redoList = new Array();

            drawCanvas();

            canvas.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
            //add button event listeners
            freeHandBtn.addEventListener(MouseEvent.CLICK, onChooseFreeHand);
            lineBtn.addEventListener(MouseEvent.CLICK, onChooseLine);
            undoBtn.addEventListener(MouseEvent.CLICK, onUndo);
            redoBtn.addEventListener(MouseEvent.CLICK, onRedo);
            clearBtn.addEventListener(MouseEvent.CLICK, onClearCanvas);
        }

        private function drawCanvas():void{
            canvas = new Sprite();
            canvas.graphics.beginFill(0xffccff, 0.2);
            canvas.graphics.drawRect(80, 60 , CANVAS_WIDTH, CANVAS_HEIGHT);
            canvas.graphics.endFill();
            addChild(canvas);
        }

        private function onMouseDownHandler(event:MouseEvent):void{
            _pressed = true;
            oldX = stage.mouseX;
            oldY = stage.mouseY;
            
            addShapes(0xffff00, statusId);
            canvas.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
            canvas.addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }

        private function addShapes(lineColor:uint, statusId:uint):void{
            this.lineColor = lineColor;
            this.statusId = statusId;
            var shapes:Shape = new Shape();
            canvas.addChild(shapes);
            shapeArr.push(shapes);
            trace(shapeArr.length - 1);
            switch(statusId){
                case FREE_HAND:
                    shapeArr[shapeArr.length - 1].graphics.lineStyle(2, lineColor);
                    shapeArr[shapeArr.length - 1].graphics.moveTo(oldX, oldY);
                    break;
                case LINE: 
                    shapeArr[shapeArr.length - 1].graphics.lineStyle(2, lineColor);
                    shapeArr[shapeArr.length - 1].graphics.moveTo(oldX, oldY);
                    break;
            }

        }

        private function onMouseMoveHandler(event:MouseEvent):void{
            switch(statusId){
                case FREE_HAND:
                    shapeArr[shapeArr.length - 1].graphics.lineTo(stage.mouseX, stage.mouseY);
                    oldX = stage.mouseX;
                    oldY = stage.mouseY;
                    break;
                case LINE: 
                    shapeArr[shapeArr.length - 1].graphics.clear();
                    shapeArr[shapeArr.length - 1].graphics.lineStyle(2, lineColor);
                    shapeArr[shapeArr.length - 1].graphics.moveTo(oldX, oldY);
                    shapeArr[shapeArr.length - 1].graphics.lineTo(stage.mouseX, stage.mouseY);
                    break;
            }
        }

        private function onMouseUpHandler(event:MouseEvent):void{
            canvas.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
            canvas.removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }

/*----------------------------button event * handlers--------------------------------*/
        private function onChooseFreeHand(e:MouseEvent):void{
            this.statusId = FREE_HAND;
        }

        private function onChooseLine(e:MouseEvent):void{
            this.statusId = LINE;
        } 

        private function onUndo(e:MouseEvent):void{
            if(shapeArr.length > 0){
                var graph:Shape = shapeArr.pop();
                canvas.removeChild(graph);
                redoList.push(graph);
                trace("undo success!");
            }
        }

        private function onRedo(e:MouseEvent):void{
            if(redoList.length > 0){
                var graph:Shape = redoList.pop();
                canvas.addChild(graph);
                shapeArr.push(graph);
                trace("redo success!");
            }
        }

        private function onClearCanvas(e:MouseEvent):void{
            while(shapeArr.length > 0){
                var graph:Shape = shapeArr.pop();
                canvas.removeChild(graph);
            }
        }
    }
}

import flash.display.Shape;
class CustomCircle extends Shape{
    public function CustomCircle():void{
        this.graphics.beginFill(0xff0000);
        this.graphics.drawCircle(0, 0, 4);
        this.graphics.endFill();
    }
}
